<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				formId:""
				
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			getworkParam(){
				return{
					formId:this.formId
				}
			},
			formSubmit(){
				let _that = this
				let showTip = false
				if(_that.subEditorObj.workInfo.workList.length){
					for(let item of _that.subEditorObj.workInfo.workList){
						if(item.adjustTypeCode === '37769' ){
							showTip = true
						}
					}
					_that.om.formSave('tmonthlyWorkAdjustApply/addTmonthlyWorkAdjustApply',_that.formdata,_that)
				}else{
					errorModal(_that, '提示信息','保存失败！数据不能为空！', function() {
						return
					})
				}
			},
			intLoad(options){
				let _that = this
				let ajaxArr = []
				let fileId = options.fileId
				_that.formId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:'tmonthlyWorkAdjustApply/getInfo',
						param:{
							id:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor('topinfo','zuserId',{
						defaultName:_that.userName,
						defalueValue:_that.userId
					},null,mainData)
					if(_that.subEditorObj["workInfo"]){
						let attr = {
							getWorkParam:"getworkParam",
						}
						let configData = _that.subConfigData["workInfo"]
						if(hasEditor('workInfo','orderNum',configData)){
							attr.titleTemplate = "<%=orderNum %>"
						}
						if(hasEditor('workInfo','workName',configData)){
							let title = getEditorTitle("workInfo","workName",configData)
							attr.subtitle1Template = title+":<%=workName %>"
						}
						if(hasEditor('workInfo','adjustTypeCode',configData)){
							let title = getEditorTitle("workInfo","adjustTypeCode",configData)
							attr.subtitle2Template = title+":<%=adjustTypeName %>"
						}
						if(hasEditor('workInfo','adjustReasonCode',configData)){
							let title = getEditorTitle("workInfo","adjustReasonCode",configData)
							attr.subtitle3Template = title+":<%=adjustReasonName %>"
						}
						changeEditor("workInfo", "objStr",attr, mainData)
					}
					if(fileId){
						let fielArr = getFormFields(mainData)
						fielArr.push('id')
						for(let index in res[0].value.tmonthlyWorkAdjustApply){
							if(fielArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[0].value.tmonthlyWorkAdjustApply[index])
								_that.$set(_that.formdata,index,res[0].value.tmonthlyWorkAdjustApply[index])
							}
						 }
						let workInfoList = _that._.cloneDeep(res[0].value.tmonthlyWorkAdjustApply_data)
						_that.subEditorObj['workInfo'].workList = workInfoList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'objStr',JSON.stringify(workInfoList))
						_that.$set(_that.formdata,'objStr',JSON.stringify(workInfoList))
						delete res[0].value.workList
					}
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.intLoad(options)
		},
		onUnload(){
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this;
			if(_that.$refs.autoform){
				for (var fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							_that.$refs.autoform
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
			}
		}
	}
</script>

<style>
</style>
